﻿using Buddy.Common;
using Buddy.Swtor;
using Buddy.Swtor.Objects;
using Buddy.Swtor.Objects.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Equipper
{
    public class CharEquip
    {
        public Dictionary<ModStat, float> StatWeights { get; set; }
        public HashSet<EquipmentSlot> EquipSlot { get; set; }
        private List<TorItem> equipList;

        public CharEquip(TorContainer<TorItem> equipment)
        {
            StatWeights = new Dictionary<ModStat, float>();
            EquipSlot = new HashSet<EquipmentSlot>();

            equipList = equipment.ToList();
            //Set current equipment (generic)
            //Ear
            setEquipment(SlotId.EquipHumanEar);
            //Implant 1
            setEquipment(SlotId.EquipHumanImplant);
            //Implant 2
            setEquipment(SlotId.EquipHumanImplant);
            //Relic 1
            setEquipment(SlotId.EquipHumanRelic);
            //Relic 2
            setEquipment(SlotId.EquipHumanRelic);
            //Head
            setEquipment(SlotId.EquipHumanFace);
            //Chest
            setEquipment(SlotId.EquipHumanChest);
            //Gloves
            setEquipment(SlotId.EquipHumanGlove);
            //Belt
            setEquipment(SlotId.EquipHumanBelt);
            //Legs
            setEquipment(SlotId.EquipHumanLeg);
            //Foot
            setEquipment(SlotId.EquipHumanFoot);

            //Stat weights
            //Main
            StatWeights.Add(ModStat.Strength, 0);
            StatWeights.Add(ModStat.Willpower, 0);
            StatWeights.Add(ModStat.Aim, 0);
            StatWeights.Add(ModStat.Cunning, 0);
            //Secondary
            StatWeights.Add(ModStat.Endurance, 8);
            StatWeights.Add(ModStat.DamageMin, 5);
            StatWeights.Add(ModStat.ArmorRating, 5);
            StatWeights.Add(ModStat.CriticalChanceRating, 3);
            StatWeights.Add(ModStat.CriticalDamageRating, 3); //Surge
            StatWeights.Add(ModStat.AttackPowerRating, 3); //Power
            StatWeights.Add(ModStat.Accuracy, 0);
            StatWeights.Add(ModStat.DefenseRating, 0);
            StatWeights.Add(ModStat.GlanceAbsorbRating, 0);
            StatWeights.Add(ModStat.AlacrityRating, 0);
            StatWeights.Add(ModStat.TechPowerRating, 0);
            StatWeights.Add(ModStat.ForcePowerRating, 0);
            StatWeights.Add(ModStat.PvpExpertiseRating, 0);


        }

        private void addClassSpecifics(CharacterClass basic, AdvancedClass advanced, CharacterDiscipline discipline)
        {
            switch (basic)
            {
                case CharacterClass.BountyHunter:
                case CharacterClass.Trooper:
                    StatWeights[ModStat.Aim] = 10; 
                    setEquipment(SlotId.EquipHumanRangedPrimary);
                    break;
                case CharacterClass.Agent:
                case CharacterClass.Smuggler:
                    StatWeights[ModStat.Cunning] = 10;
                    setEquipment(SlotId.EquipHumanRangedPrimary);
                    break;
                case CharacterClass.Inquisitor:
                case CharacterClass.Consular:
                    StatWeights[ModStat.Willpower] = 10;
                    setEquipment(SlotId.EquipHumanMainHand);
                    break;
                case CharacterClass.Warrior:
                case CharacterClass.Knight:
                    StatWeights[ModStat.Strength] = 10;
                    setEquipment(SlotId.EquipHumanMainHand);
                    break;
            }

            switch (advanced)
            {
                case AdvancedClass.Mercenary:
                    setEquipment(SlotId.EquipHumanRangedSecondary);
                    break;
                case AdvancedClass.Powertech:
                case AdvancedClass.Vanguard:
                case AdvancedClass.Sentinel:
                case AdvancedClass.Juggernaut:
                    setEquipment(SlotId.EquipHumanShield);
                    break;
                case AdvancedClass.Operative:
                case AdvancedClass.Sniper:
                    setEquipment(SlotId.EquipHumanCustomMelee);
                    break;
                case AdvancedClass.Marauder:
                    setEquipment(SlotId.EquipHumanOffHand);
                    break;
            }

            //TODO
            switch (discipline)
            {
                //Tanks
                case CharacterDiscipline.Darkness:
                case CharacterDiscipline.ShieldTech:
                case CharacterDiscipline.Immortal:
                case CharacterDiscipline.ShieldSpecialist:
                case CharacterDiscipline.KeneticCombat:
                case CharacterDiscipline.Defense:
                    break;
                case CharacterDiscipline.Pyrotech:
                case CharacterDiscipline.AdvancedPrototype:
                    break;
            }
        }

        private void setEquipment(SlotId slotId)
        {
            TorItem item;
            //Check that equipList still has items, due to players not having every slot equipped
            if (equipList.Any())
            {
                item = equipList.FirstOrDefault(f => f.SlotTypes.Contains(slotId));
            }
            else
            {
                item = null;
            }
            EquipSlot.Add(new EquipmentSlot(slotId, item));
            //Remove item from list so we don't grab it again (eg. ears, relics)
            equipList.Remove(item);
        }

    }

    public class EquipmentSlot
    {
        public SlotId Slot { get; set; }
        public TorItem Item { get; set; }
        public float Weight { get; set; }
        public EquipmentSlot(SlotId s, TorItem i)
        {
            Slot = s;
            Item = i;
        }
    }
}
